<template>
    <div style="margin-top:100px;width:650px;">
        <el-form :model="ruleForm" label-width="100px" :rules="rules" ref="ruleForm">
            <el-form-item label="邮箱地址" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
                <el-input type="password" v-model="ruleForm.password"></el-input>
            </el-form-item>
            <el-form-item label="">
                <el-button type="primary" @click="login('ruleForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script>
import * as types from '@/store/types'
export default {
    data() {
        return {
            app: this.$cloudbase,
            hasLoginState:  this.$cloudbase.auth().hasLoginState(),
            ruleForm:{
                name: "3194781099@qq.com",
                password: "a12345678",
            },
            rules:{
                name:{required:true,message:'请填写邮箱',trigger:'blur'},
                password:[{required:true,message:'请填写密码',trigger:'blur'},{ min: 8, max: 32, message: '密码长度不小于 8 位，不大于 32 位，需要包含字母和数字', trigger: 'blur' }]
            }
        }
    },
    mounted() {
    },
    methods: {
        login(formName){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.app
                    .auth({persistence: "local"})
                    .signInWithEmailAndPassword(this.ruleForm.name,this.ruleForm.password)
                    .then(res=>{
                        if(res.user){
                            const logindata = {
                                email: res.user.email,
                                uid: res.user.uid
                            }
                            
                            this.$store.commit(types.LOGIN, logindata)

                            // const user = { email: res.user.email, uid: res.user.uid};
                            // localStorage.user = JSON.stringify(user)

                            // this.$router.push('/admin/default')
                            let redirect = decodeURIComponent(
                                this.$route.query.redirect || "/"
                            );
                            this.$router.push({
                                path: redirect
                            });
                            // this.getMenuData()
                        }
                    }).catch(err=>{
                        this.$message.error(err)
                    })
                } else {
                    // console.log('error submit!!');
                    return false;
                }
            })
        },
        reg(formName){
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    this.app
                    .auth()
                    .signUpWithEmailAndPassword(this.ruleForm.name,this.ruleForm.password)
                    .then(res=>{
                        if(res.requestId){
                            this.$message.success("请登录注册邮箱，进行验证! 验证完成，点击登录")
                        }
                    }).catch(err=>{
                        console.log(err)
                    })
                } else {
                    // console.log('error submit!!');
                    return false;
                }
            })
        },
    },
    components: {},
}
</script>

<style scoped>

</style>